# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid

from rest_framework.views import APIView
from components.kubernetes.dao import save_k8s_cluster_hosts, get_kubernetes_manage_hosts
from utils.views import Result

class ClusterHostsManage(APIView):
    def post(self, request):
        data = request.data
        kubernetes_cluster_id = data['kubernetes_cluster_id']

        kubernetes_manage_data = get_kubernetes_manage_hosts(data['host_ids'])

        for kubernetes_manage_data_one in kubernetes_manage_data:
            if kubernetes_manage_data_one['kubernetes_role']:
                if kubernetes_cluster_id != kubernetes_manage_data_one['kubernetes_cluster_id']:
                    return Result.success_response(msg="已有角色主机无法转移到其他集群，请检查您的勾选项！")

        for host_id in data['host_ids']:
            deploy_event_ids = uuid.uuid1()
            for kubernetes_manage_data_one in kubernetes_manage_data:
                if host_id == kubernetes_manage_data_one['host_id']:
                    if kubernetes_manage_data_one['deploy_event_ids']:
                        deploy_event_ids = kubernetes_manage_data_one['deploy_event_ids']

            save_k8s_cluster_hosts(kubernetes_cluster_id, host_id, deploy_event_ids)

        return Result.success_response(msg='执行成功')